package com.cencat.controller;

import com.cencat.domain.Student;

import com.cencat.service.StudentService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("/student")
public class controller {

    @Autowired
    StudentService studentService;

    /*查询全部*/
    @RequestMapping("/findAll")
    public String list(Model model){
        List<Student> students = studentService.getAll();
        students.forEach(System.out::println);
        model.addAttribute("students", students);
        return "students";
    }

    /*新增*/
    @RequestMapping("/add")
    public String add(Student student, Model model){
        studentService.addStudent(student);
        List<Student> students = new ArrayList<>();
        model.addAttribute("students", students);
        return "students";
    }

    /*通过id修改*/
    @RequestMapping("/updateById")
    public String updateById(Student student, Model model){
        studentService.updateStudent(student);
        List<Student> students = new ArrayList<>();
        model.addAttribute("students", students);
        return "students";
    }

    /*通过id删除*/
    @RequestMapping("/deleteById")
    public String deleteById(int id, Model model){
        studentService.deleteStudent(id);
        List<Student> students = new ArrayList<>();
        model.addAttribute("students", students);
        return "students";
    }

    /*通过id查询*/
    @RequestMapping("/findById")
    public String findById(Integer id, Model model){
        Student student = studentService.getStudentById(id);
        List<Student> students = new ArrayList<>();
        students.add(student);
        model.addAttribute("students", students);
        return "students";
    }

    @RequestMapping("/findByPage")
    public String findByPage(int page, int size, Model model){
        List<Student> students = studentService.getStudentByPage(page, size);
        model.addAttribute("students", students);
        return "students";
    }

}

